<template>
	<view class="detail-container">
		<!-- 导航栏 -->
		<view class="nav-bar">
			<view class="nav-back" @click="goBack">
				<text class="iconfont icon-back">‹</text>
			</view>
			<view class="nav-title">详情页面</view>
			<view class="nav-right"></view>
		</view>

		<!-- 内容区域 -->
		<scroll-view class="content-scroll" scroll-y="true">
			<!-- 头部信息 -->
			<view class="header-section">
				<view class="title">{{ detailInfo.title || '示例标题' }}</view>
				<view class="meta-info">
					<text class="time">{{ detailInfo.createTime || '2024-01-01' }}</text>
					<text class="author">{{ detailInfo.author || '作者' }}</text>
				</view>
			</view>

			<!-- 图片展示 -->
			<view class="image-section" v-if="detailInfo.images && detailInfo.images.length">
				<swiper class="image-swiper" :indicator-dots="true" :autoplay="false">
					<swiper-item v-for="(image, index) in detailInfo.images" :key="index">
						<image :src="image" mode="aspectFill" @click="previewImage(image)"></image>
					</swiper-item>
				</swiper>
			</view>

			<!-- 内容详情 -->
			<view class="content-section">
				<rich-text :nodes="detailInfo.content || defaultContent"></rich-text>
			</view>

			<!-- 标签 -->
			<view class="tags-section" v-if="detailInfo.tags && detailInfo.tags.length">
				<view class="tag-item" v-for="(tag, index) in detailInfo.tags" :key="index">
					{{ tag }}
				</view>
			</view>

			<!-- 操作按钮 -->
			<view class="action-section">
				<button class="action-btn primary" @click="handleAction('collect')">
					{{ isCollected ? '已收藏' : '收藏' }}
				</button>
				<button class="action-btn secondary" @click="handleAction('share')">
					分享
				</button>
			</view>

			<!-- 相关推荐 -->
			<view class="recommend-section" v-if="recommendList.length">
				<view class="section-title">相关推荐</view>
				<view class="recommend-list">
					<view class="recommend-item" v-for="(item, index) in recommendList" :key="index" 
						@click="goToDetail(item.id)">
						<image :src="item.image" mode="aspectFill"></image>
						<view class="item-info">
							<view class="item-title">{{ item.title }}</view>
							<view class="item-time">{{ item.createTime }}</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 底部操作栏 -->
		<view class="bottom-bar">
			<view class="bar-item" @click="handleAction('like')">
				<text class="iconfont" :class="isLiked ? 'icon-like-fill' : 'icon-like'">♡</text>
				<text class="bar-text">{{ likeCount }}</text>
			</view>
			<view class="bar-item" @click="handleAction('comment')">
				<text class="iconfont icon-comment">💬</text>
				<text class="bar-text">评论</text>
			</view>
			<button class="contact-btn" @click="handleAction('contact')">
				联系我们
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detailId: '',
				detailInfo: {},
				isCollected: false,
				isLiked: false,
				likeCount: 0,
				recommendList: [],
				defaultContent: '<p>这是一个示例详情页面，您可以根据实际需求修改内容结构和样式。</p><p>支持富文本内容展示，包括文字、图片、链接等。</p>'
			}
		},
		onLoad(options) {
			this.detailId = options.id || '';
			this.loadDetailInfo();
			this.loadRecommendList();
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},
			
			// 加载详情信息
			async loadDetailInfo() {
				try {
					// 这里应该调用实际的API接口
					// const res = await this.$api.getDetailInfo(this.detailId);
					
					// 示例数据
					this.detailInfo = {
						title: '示例详情标题',
						content: '<p>这是详情内容，支持HTML格式。</p><p>您可以在这里展示丰富的内容信息。</p>',
						createTime: '2024-01-01 12:00:00',
						author: '示例作者',
						images: [
							'/static/images/example1.jpg',
							'/static/images/example2.jpg'
						],
						tags: ['标签1', '标签2', '标签3']
					};
					
					this.likeCount = Math.floor(Math.random() * 100);
				} catch (error) {
					console.error('加载详情失败:', error);
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					});
				}
			},
			
			// 加载推荐列表
			async loadRecommendList() {
				try {
					// 示例推荐数据
					this.recommendList = [
						{
							id: '1',
							title: '推荐内容1',
							image: '/static/images/recommend1.jpg',
							createTime: '2024-01-02'
						},
						{
							id: '2',
							title: '推荐内容2',
							image: '/static/images/recommend2.jpg',
							createTime: '2024-01-03'
						}
					];
				} catch (error) {
					console.error('加载推荐失败:', error);
				}
			},
			
			// 预览图片
			previewImage(current) {
				uni.previewImage({
					current: current,
					urls: this.detailInfo.images
				});
			},
			
			// 处理各种操作
			handleAction(type) {
				switch (type) {
					case 'collect':
						this.toggleCollect();
						break;
					case 'share':
						this.shareContent();
						break;
					case 'like':
						this.toggleLike();
						break;
					case 'comment':
						this.showComment();
						break;
					case 'contact':
						this.contactUs();
						break;
				}
			},
			
			// 切换收藏状态
			toggleCollect() {
				this.isCollected = !this.isCollected;
				uni.showToast({
					title: this.isCollected ? '收藏成功' : '取消收藏',
					icon: 'success'
				});
			},
			
			// 分享内容
			shareContent() {
				// #ifdef MP-WEIXIN
				uni.showShareMenu({
					withShareTicket: true
				});
				// #endif
				
				// #ifndef MP-WEIXIN
				uni.showActionSheet({
					itemList: ['分享到微信', '分享到朋友圈', '复制链接'],
					success: (res) => {
						console.log('选择分享方式:', res.tapIndex);
					}
				});
				// #endif
			},
			
			// 切换点赞状态
			toggleLike() {
				this.isLiked = !this.isLiked;
				this.likeCount += this.isLiked ? 1 : -1;
				uni.showToast({
					title: this.isLiked ? '点赞成功' : '取消点赞',
					icon: 'success'
				});
			},
			
			// 显示评论
			showComment() {
				uni.showToast({
					title: '评论功能待开发',
					icon: 'none'
				});
			},
			
			// 联系我们
			contactUs() {
				uni.showModal({
					title: '联系我们',
					content: '请通过以下方式联系我们：\n电话：400-000-0000\n邮箱：contact@example.com',
					showCancel: false
				});
			},
			
			// 跳转到详情页
			goToDetail(id) {
				uni.navigateTo({
					url: `/subPages/example/detail?id=${id}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f8f9fa;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 88rpx;
		padding: 0 30rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
		position: sticky;
		top: 0;
		z-index: 100;

		.nav-back {
			width: 60rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			
			.icon-back {
				font-size: 36rpx;
				color: #333;
			}
		}

		.nav-title {
			font-size: 32rpx;
			font-weight: 600;
			color: #333;
		}

		.nav-right {
			width: 60rpx;
		}
	}

	.content-scroll {
		flex: 1;
		padding-bottom: 120rpx;
	}

	.header-section {
		padding: 40rpx 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;

		.title {
			font-size: 36rpx;
			font-weight: 600;
			color: #333;
			line-height: 1.4;
			margin-bottom: 20rpx;
		}

		.meta-info {
			display: flex;
			align-items: center;
			gap: 30rpx;

			.time, .author {
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	.image-section {
		margin-bottom: 20rpx;

		.image-swiper {
			height: 400rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.content-section {
		padding: 40rpx 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		line-height: 1.6;
	}

	.tags-section {
		padding: 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;

		.tag-item {
			padding: 10rpx 20rpx;
			background-color: #f0f0f0;
			border-radius: 20rpx;
			font-size: 24rpx;
			color: #666;
		}
	}

	.action-section {
		padding: 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		display: flex;
		gap: 30rpx;

		.action-btn {
			flex: 1;
			height: 80rpx;
			border-radius: 40rpx;
			font-size: 28rpx;
			border: none;

			&.primary {
				background-color: #007aff;
				color: #fff;
			}

			&.secondary {
				background-color: #f8f9fa;
				color: #333;
				border: 1rpx solid #ddd;
			}
		}
	}

	.recommend-section {
		background-color: #fff;
		margin-bottom: 20rpx;

		.section-title {
			padding: 30rpx;
			font-size: 32rpx;
			font-weight: 600;
			color: #333;
			border-bottom: 1rpx solid #eee;
		}

		.recommend-list {
			padding: 0 30rpx;

			.recommend-item {
				display: flex;
				align-items: center;
				padding: 30rpx 0;
				border-bottom: 1rpx solid #f0f0f0;

				&:last-child {
					border-bottom: none;
				}

				image {
					width: 120rpx;
					height: 80rpx;
					border-radius: 8rpx;
					margin-right: 20rpx;
				}

				.item-info {
					flex: 1;

					.item-title {
						font-size: 28rpx;
						color: #333;
						margin-bottom: 10rpx;
					}

					.item-time {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}

	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 120rpx;
		background-color: #fff;
		border-top: 1rpx solid #eee;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		gap: 30rpx;

		.bar-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 8rpx;

			.iconfont {
				font-size: 40rpx;
				color: #666;
			}

			.bar-text {
				font-size: 20rpx;
				color: #666;
			}
		}

		.contact-btn {
			flex: 1;
			height: 70rpx;
			background-color: #007aff;
			color: #fff;
			border: none;
			border-radius: 35rpx;
			font-size: 28rpx;
		}
	}
</style>
